package cn.itcast.bos.web.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.apache.commons.io.FileUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import cn.itcast.bos.domain.constant.Constants;
import cn.itcast.bos.domain.page.PageBean;
import cn.itcast.bos.domain.take_delivery.Promotion;
import cn.itcast.bos.web.action.common.BaseAction;
import freemarker.template.Configuration;
import freemarker.template.Template;

@SuppressWarnings("all")
@Namespace("/")
@ParentPackage("struts-default")
@Scope("prototype")
@Controller
public class PromotionAction extends BaseAction<Promotion> {
	private static final long serialVersionUID = 1L;

	// 活动前端页面的分页
	@Action(value = "promotion_pageQuery")
	public void pageQuery() throws Exception {
		PageBean<Promotion> pageBean = WebClient.create(
				Constants.BOS_MANAGEMENT_URL + "/services/promotionService/pageQuery?page=" + page + "&rows=" + rows)
				.accept(MediaType.APPLICATION_JSON).get(PageBean.class);
		String jsonString = JSONObject.toJSONString(pageBean, SerializerFeature.DisableCircularReferenceDetect);
		ServletActionContext.getResponse().setContentType("application/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().write(jsonString);
	}

	// 活动详情页面
	@Action(value = "promotion_showDetail")
	public void showDetail() throws Exception {
		// 先判断id所对应的html是否存在,如果不存在就直接创建,如果存在就直接返回
		String realPath = ServletActionContext.getServletContext().getRealPath("/freemarker");
		File htmlFile = new File(realPath + "/" + model.getId() + ".html");
		// 如果文件不存在,创建文件
		if (!htmlFile.exists()) {
			Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
			configuration.setDirectoryForTemplateLoading(
					new File(ServletActionContext.getServletContext().getRealPath("/WEB-INF/freemarker_templates")));
			// 获取模板对象
			Template template = configuration.getTemplate("promotion_detail.ftl");
			// 获取模型数据
			Promotion promotion = WebClient
					.create(Constants.BOS_MANAGEMENT_URL + "/services/promotionService/promotion/" + model.getId())
					.accept(MediaType.APPLICATION_JSON).get(Promotion.class);
			// 将模型数据封装到集合中
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("promotion", promotion);
			// 合并输出
			template.process(map, new OutputStreamWriter(new FileOutputStream(htmlFile), "utf-8"));
		}
		// 如果文件存在
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		FileUtils.copyFile(htmlFile, ServletActionContext.getResponse().getOutputStream());
	}
}
